package com.party.mobile.web.controller.crowdfund;

import com.party.authorization.annotation.Authorization;
import com.party.common.paging.Page;
import com.party.common.utils.PartyCode;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.mobile.biz.crowdfund.CrowdfundEventBizService;
import com.party.mobile.biz.crowdfund.ProjectBizService;
import com.party.mobile.biz.member.MemberBizService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.crowdfund.input.CrowdfundEventHomeInput;
import com.party.mobile.web.dto.crowdfund.input.PhoneVerifyInput;
import com.party.mobile.web.dto.crowdfund.output.CrowdfundEventHomeOutput;
import com.party.mobile.web.dto.crowdfund.output.CrowdfundEventOutput;
import com.party.mobile.web.dto.login.output.CurrentUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 众筹主题控制器
 * Created by wei.li.
 *
 * @date 2018/1/16 0016
 * @time 16:04
 */
@Controller
@RequestMapping(value = "/crowdfund/event")
public class CrowdfundEventController {

    @Autowired
    private CrowdfundEventBizService crowdfundEventBizService;

    @Autowired
    private ProjectBizService projectBizService;

    @Autowired
    private MemberBizService memberBizService;

    protected static Logger logger = LoggerFactory.getLogger(CrowdfundEventController.class);

    /**
     * 获取众筹主题详情
     * @param id 编号
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "detail")
    public AjaxResult detail(String id){
        AjaxResult ajaxResult = new AjaxResult(true);
        CrowdfundEventOutput output = null;
        try {
            output = crowdfundEventBizService.detail(id);
        } catch (BusinessException e) {
            logger.error("获取众筹详情主题异常", e);
            return AjaxResult.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("获取众筹详情主题异常", e);
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription("获取众筹详情主题异常");
            return ajaxResult;
        }
        ajaxResult.setData(output);
        return ajaxResult;
    }


    /**
     * 主页显示众筹主题列表
     * @param page 分页参数
     * @param input 查询参数
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "home/list")
    public AjaxResult homeList(Page page, CrowdfundEventHomeInput input){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<CrowdfundEventHomeOutput> outputList = crowdfundEventBizService.homeList(page, input);
            return AjaxResult.success(outputList, page);
        } catch (Exception e) {
            logger.error("主页显示众筹主题列表异常", e);
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription("主页显示众筹主题列表异常");
            return ajaxResult;
        }
    }

    /**
     * 获取主题信息
     *
     * @param eventId 主题id
     * @return 主题信息
     */
    @ResponseBody
    @RequestMapping("getEventSimpleInfo")
    public AjaxResult getEventSimpleInfo(String eventId) {
        if (StringUtils.isEmpty(eventId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "主题id不能为空");
        }
        try {
            CrowdfundEventOutput simpleInfo = crowdfundEventBizService.getEventSimpleInfo(eventId);
            return AjaxResult.success(simpleInfo);
        } catch (BusinessException e) {
            return AjaxResult.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("获取主题信息异常", e);
            return AjaxResult.error(100, "获取主题信息异常");
        }
    }

    /**
     * 绑定手机号
     *
     * @param verifyInput 输入参数
     * @param result      验证结果
     * @param request     request
     * @return 如果用户该主题下有报名，返回金额最大的众筹id，没有则返回空
     */
    @ResponseBody
    @Authorization
    @RequestMapping("verifyMobile")
    public AjaxResult verifyMobile(@Validated PhoneVerifyInput verifyInput, BindingResult result, HttpServletRequest request) {
        //数据验证
        if (result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            String description = allErrors.get(0).getDefaultMessage();
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, description);
        }

        try {
            CurrentUser currentUser = memberBizService.bindPhoneBiz2(verifyInput.getMobile(), verifyInput.getVerifyCode(),
                    verifyInput.getCountryCode(), request);
            String projectId = projectBizService.getProjectId(currentUser.getId(), verifyInput.getId());
            return AjaxResult.success((Object) projectId);
        } catch (BusinessException e) {
            return AjaxResult.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("绑定手机号异常", e);
            return AjaxResult.error(100, "绑定手机号异常");
        }
    }
}
